package tr.edu.yildiz.ce.dap.appagent.command.deployment.agent;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;

import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.AbstractAgentCommand;
import tr.edu.yildiz.ce.dap.appagent.command.deployment.base.CommandConstants;
import tr.edu.yildiz.ce.dap.appagent.exception.AgentDeploymentException;
import tr.edu.yildiz.ce.dap.appagent.exception.AgentRetryException;
import tr.edu.yildiz.ce.dap.appagent.exception.BasicAgentException;
import tr.edu.yildiz.ce.dap.appagent.manager.ApplicationManager;
import tr.edu.yildiz.ce.dap.appagent.model.Actions;
import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.ce.dap.appagent.model.DapMessage;
import tr.edu.yildiz.ce.dap.appagent.process.DeploymentProcess;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;
import tr.edu.yildiz.ce.dap.appagent.service.infra.ResourceManager;
import tr.edu.yildiz.ce.dap.appagent.util.AgentProperties;
import tr.edu.yildiz.ce.dap.appagent.util.DeploymentConstants;
import tr.edu.yildiz.ce.dap.appagent.util.DeploymentUtils;

public class RetrieveCommand extends AbstractAgentCommand {

	String applicationName ;
	String applicationVersion ;
	
	public RetrieveCommand(AbstractProcess process,String applicationName,String applicationVersion) {
		super(process);
		this.applicationName = applicationName;
		this.applicationVersion = applicationVersion;
	}

	@Override
	public void prepareRequest(DapMessage msg) throws BasicAgentException{
		msg.setCommand(CommandConstants.RETRIEVE);
		msg.setAppName(applicationName);
		msg.setAppVersion(applicationVersion);
	}

	@Override
	public void processResponse(DapMessage msg)
			throws BasicAgentException, AgentRetryException {
		String applicationName = msg.getAppName(); 
		List<Component> cList = msg.getComponentList().getComponent();
		for (Component component : cList) {
			String bundleLocation = DeploymentUtils.downloadBundle(component);
			extractResourceIfNecessary(bundleLocation,applicationName);
			DeploymentUtils.installBundle(component, bundleLocation,getProcess().getProcessManager().getContext());
		}
		ApplicationManager.addInstalledApplication(msg);
	}
	

	private void extractResourceIfNecessary(String bundleLocation,String applicationName) {
		ServiceReference sRef = getProcess().getProcessManager().getContext().getServiceReference(ResourceManager.class.getName());
		if (sRef != null){
			ResourceManager resourceManager = (ResourceManager) getProcess().getProcessManager().getContext().getService(sRef);
			try {
				resourceManager.initializeResourcesFolder(bundleLocation,applicationName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public DapMessage rollback(DapMessage msg) throws BasicAgentException {
		// TODO Auto-generated method stub
		return null;
	}

	
}
